package com.zjw.utils;

import com.zjw.annotation.domain.entity.User;
import com.zjw.service.impl.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.List;

/**
 * Security工具类
 *
 * @author LKZY
 * @date 2023/12/15 10:32
 */
public class SecurityUtil {

    /**
     * 获取身份验证
     *
     * @return {@link Authentication}
     */
    public static Authentication getCurAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取授权主体对象
     *
     * @return {@link LoginUser}
     */
    public static LoginUser getCurPrincipal() {
        try {
            return (LoginUser) getCurAuthentication().getPrincipal();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取当前登录用户实体对象
     *
     * @return {@link User}
     */
    public static User getCurUser() {

        try {
            return getCurPrincipal().getUser();
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 获取当前登录用户id
     *
     * @return {@link Long}
     */
    public static Long getCurUserId() {
        try {
            return getCurPrincipal().getUser().getId();
        } catch (Exception e) {
            return -1L;
        }
    }

    /**
     * 获取当前用户的角色和权限集合用于权限判断
     *
     * @return {@link String}
     */
    public static final List<String> getCurUserAuthorities(){
        return getCurPrincipal().getMyAuthorities();
    }



}
